﻿using System;
using System.Globalization;
using Monkey.Warrenty.Biz;

namespace MonkeyWarranty.Web.Reports
{
    public partial class InstantTicketReport : System.Web.UI.Page
    {
        #region Properties

        private static InstantTicketBiz _instantTicketBiz;
        private static InstantTicketBiz InstantTicketManager
        {
            get { return _instantTicketBiz ?? (_instantTicketBiz = new InstantTicketBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                var dtDateFrom = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
                var dtDateTo = DateTime.Today;
                txtDateFrom.Text = string.Format("{0:dd/MM/yyyy}", dtDateFrom);
                txtDateTo.Text = string.Format("{0:dd/MM/yyyy}", dtDateTo);
                LoadData(dtDateFrom, dtDateTo);
            }
        }

        protected void btnLoad_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            
            DateTime dtDateFrom, dtDateTo;
            if(!DateTime.TryParseExact(txtDateFrom.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtDateFrom))
            {
                lblErrorMessage.Text = Resources.GlobalStrings.InvalidDate;
                return;
            }

            if (!DateTime.TryParseExact(txtDateTo.Text.Trim(), "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtDateTo))
            {
                lblErrorMessage.Text = Resources.GlobalStrings.InvalidDate;
                return;
            }
            
            LoadData(dtDateFrom, dtDateTo);
        }

        #endregion

        #region Helpers

        private void LoadData(DateTime dtDateFrom, DateTime dtDateTo)
        {
            ltrPeriod.Text = string.Format("{0:dd/MM/yyyy} - {1:dd/MM/yyyy}", dtDateFrom, dtDateTo);
            var ticketEmployeeCounts = InstantTicketManager.GetInstantTicketEmployeeCount(dtDateFrom, dtDateTo);
            rptTicketEmployeeCounts.DataSource = ticketEmployeeCounts;
            rptTicketEmployeeCounts.DataBind();
        }

        #endregion
    }
}